// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.opengl;

import java.io.PrintStream;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL11;
import org.lwjglx.opengl.OpenGLException;
import org.lwjglx.opengl.Util;
import zombie.core.skinnedmodel.model.Model;

public class PZGLUtil {
    static int test = 0;

    public static void checkGLErrorThrow(String string, Object... object) throws OpenGLException {
        int _int = GL11.glGetError();
        if (_int != 0) {
            test++;
            throw new OpenGLException(createErrorMessage(_int, string, object));
        }
    }

    private static String createErrorMessage(int _int, String string1, Object... object) {
        String string0 = System.lineSeparator();
        return "  GL Error code ("
            + _int
            + ") encountered."
            + string0
            + "  Error translation: "
            + createErrorMessage(_int)
            + string0
            + "  While performing: "
            + String.format(string1, object);
    }

    private static String createErrorMessage(int _int) {
        String string = Util.translateGLErrorString(_int);
        return string + " (" + _int + ")";
    }

    public static boolean checkGLError(boolean stackTrace) {
        try {
            Util.checkGLError();
            return true;
        } catch (OpenGLException openGLException) {
            RenderThread.logGLException(openGLException, stackTrace);
            return false;
        }
    }

    public static void printGLState(PrintStream out) {
        int _int = GL11.glGetInteger(2979);
        out.println("DEBUG: GL_MODELVIEW_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(2980);
        out.println("DEBUG: GL_PROJECTION_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(2981);
        out.println("DEBUG: GL_TEXTURE_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(2992);
        out.println("DEBUG: GL_ATTRIB_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(2993);
        out.println("DEBUG: GL_CLIENT_ATTRIB_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(3381);
        out.println("DEBUG: GL_MAX_ATTRIB_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(3382);
        out.println("DEBUG: GL_MAX_MODELVIEW_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(3383);
        out.println("DEBUG: GL_MAX_NAME_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(3384);
        out.println("DEBUG: GL_MAX_PROJECTION_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(3385);
        out.println("DEBUG: GL_MAX_TEXTURE_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(3387);
        out.println("DEBUG: GL_MAX_CLIENT_ATTRIB_STACK_DEPTH= " + _int);
        _int = GL11.glGetInteger(3440);
        out.println("DEBUG: GL_NAME_STACK_DEPTH= " + _int);
    }

    public static void loadMatrix(Matrix4f matrix4f) {
        matrix4f.get(Model.m_staticReusableFloatBuffer);
        Model.m_staticReusableFloatBuffer.position(16);
        Model.m_staticReusableFloatBuffer.flip();
        GL11.glLoadMatrixf(Model.m_staticReusableFloatBuffer);
    }

    public static void multMatrix(Matrix4f matrix4f) {
        matrix4f.get(Model.m_staticReusableFloatBuffer);
        Model.m_staticReusableFloatBuffer.position(16);
        Model.m_staticReusableFloatBuffer.flip();
        GL11.glMultMatrixf(Model.m_staticReusableFloatBuffer);
    }

    public static void loadMatrix(int _int, Matrix4f matrix4f) {
        GL11.glMatrixMode(_int);
        loadMatrix(matrix4f);
    }

    public static void multMatrix(int _int, Matrix4f matrix4f) {
        GL11.glMatrixMode(_int);
        multMatrix(matrix4f);
    }

    public static void pushAndLoadMatrix(int _int, Matrix4f matrix4f) {
        GL11.glMatrixMode(_int);
        GL11.glPushMatrix();
        loadMatrix(matrix4f);
    }

    public static void pushAndMultMatrix(int _int, Matrix4f matrix4f) {
        GL11.glMatrixMode(_int);
        GL11.glPushMatrix();
        multMatrix(matrix4f);
    }

    public static void popMatrix(int mode) {
        GL11.glMatrixMode(mode);
        GL11.glPopMatrix();
    }
}
